package cs271.examples.oop;

/**
 * A subclass of MySuperClass used to demonstrate some features of inheritance.
 *
 * @author mbone
 */
public class MySubClass extends MySuperClass{

    
    public int getProtectedInt() {
        //super is a reference to our superclass
        return super.protectedInt;
    }
    
    public void setProtectedInt(int protectedInt) {
        //but I can actually refer to it with this
        this.protectedInt = protectedInt;
        //however, I would argue that using the super keyword
        //makes the code much more readable
    }
    
    public String getMyPrivateString() {
        //here we see where the reference to the super class is useful
        //I'm overriding the method here to 'double' the string returned by the 
        //super class
        return super.getMyPrivateString() + super.getMyPrivateString();
        /* Notice that I cannot access the myPrivateString variable directly
         * using the this.myPrivateString or the super.myPrivateString.
         * Why? Because of the private modifier in the superclass.
         *
         * Also notice that I called the super.getMyPrivateString(), again, this
         * calls the getMyPrivateStringMethod() in the super class.  
         *
         * If I had simply done return getMyPrivateString() or 
         * return this.getMyPrivateString(), the method would've simply called 
         * itself, going into infinite recursion and crashing the program.
         *
         */
    }
    
    
    public static void main(String[] args) {
        MySuperClass zuper = new MySuperClass();
        zuper.setMyPrivateString("Hello World.");
        System.out.println(zuper.getMyPrivateString());
        
        MySubClass sub = new MySubClass();
        sub.setProtectedInt(5);
        sub.setMyPrivateString("Hello World.");
        System.out.println(sub.getMyPrivateString());
    }
}
